package graphNew;

import java.awt.Paint;
import java.awt.Dimension;
import java.util.Vector;

import javax.swing.JFrame;


import main.Translating;

import org.apache.commons.collections15.Transformer;

import org.jgrapht.graph.*;


import components.Operator;
import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;
/**
 * 
 * @author dannysi
 *The main graph and the way it looks like after the input
 */
public class MainGraph {
	
	private DefaultDirectedWeightedGraph<Operator, DefaultWeightedEdge> maingraph;
	
	
	/**
	 * creates the main graph
	 * @param operators all the operators == vertexes in the graph
	 */
	public MainGraph(Vector<Operator> operators){
		maingraph=new DefaultDirectedWeightedGraph<Operator, DefaultWeightedEdge>(DefaultWeightedEdge.class);
		//VertexAdd
		for (Operator i: operators)
			maingraph.addVertex(i);
		//edges add
		for (Operator i:maingraph.vertexSet()){
			for (Operator j:maingraph.vertexSet()){
				if ((i!=j)&& (!maingraph.containsEdge(i, j))){
					if (Operator.checkRelation(i,j)){
						DefaultWeightedEdge e=maingraph.addEdge(i, j);
						maingraph.setEdgeWeight(e, i.getWeight());
					}
						
				}
			}
		}
		
		
			
			
		
		
	}
	/**
	 * GUI
	 */
	public void printMainGraph(){
		Graph jungGraph = Translating.ourGraphToJungGraph(this);
		
		// The Layout<V, E> is parameterized by the vertex and edge types
		Layout<Operator, DefaultWeightedEdge> layout = new FRLayout<Operator, DefaultWeightedEdge>(jungGraph);
		layout.setSize(new Dimension(500,500)); // sets the initial size of the space
		
		// The BasicVisualizationServer<V,E> is parameterized by the edge types
		VisualizationViewer<Operator,DefaultWeightedEdge> vv =
		new VisualizationViewer<Operator,DefaultWeightedEdge>(layout);
		vv.setPreferredSize(new Dimension(600,600)); //Sets the viewing area size
		
		//transformer design pattern for painting sink / source differently
		Transformer<Operator,Paint> vertexPaint = new Transformer<Operator,Paint>() {
			public Paint transform(Operator op) {
				return op.getPaintingColor();
			}
		};
		
		//Vertices and edges view editing
		vv.getRenderContext().setVertexFillPaintTransformer(vertexPaint);
		vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<Operator>());
		//vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<DefaultWeightedEdge>());
		vv.getRenderer().getVertexLabelRenderer().setPosition(Position.CNTR);
		
		//iterative mouse for ZOOM IN/OUT
		DefaultModalGraphMouse<Operator,DefaultWeightedEdge> gm = 
			new DefaultModalGraphMouse<Operator,DefaultWeightedEdge>();
		gm.setMode(ModalGraphMouse.Mode.TRANSFORMING);
		vv.setGraphMouse(gm);
		
		//graphic output / visualization
		JFrame frame = new JFrame("Main Graph");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(vv);
		frame.pack();
		frame.setVisible(true);		
	}
	
	
	/**
	 * getter
	 * @return the main graph 
	 * @throws Exception if the user didnt set a maingraph yet and it didnt created
	 */
	public DefaultDirectedWeightedGraph<Operator, DefaultWeightedEdge> getMainGraph() throws Exception{
		if (maingraph==null) throw new Exception("set main graph first");
			return maingraph;
		
	}


	

}
